package com.example.asus.testskip;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class SkipActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mUserNameEdt, mPasswordEdt, mBalanceEdt;
    private Button mJumpBtn;
    private String userName, password;
    private int gameType = 6;//invariable
    private String webId = "-1";//invariable
    private String language = "zh";//zh,en ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mUserNameEdt = (EditText) findViewById(R.id.edt_name);
        mPasswordEdt = (EditText) findViewById(R.id.edt_password);
        mBalanceEdt = (EditText) findViewById(R.id.edt_balance);
        mJumpBtn = (Button) findViewById(R.id.btn_jump);
        mJumpBtn.setOnClickListener(this);
        register();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mMyReceiver);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_jump:
                register();
                break;
        }
    }

    MyReceiver mMyReceiver;

    public void register() {
        mMyReceiver = new MyReceiver();
        Log.e("register", "registerReceiver() mMyReceiver=" + mMyReceiver);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        intentFilter.setPriority(Integer.MAX_VALUE);
        Intent intent = registerReceiver(mMyReceiver, intentFilter);
        Log.e("register", "registerReceiver() intent=" + intent);
    }

    private void startJump() {
        userName = mUserNameEdt.getText().toString().trim();
        password = mPasswordEdt.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            Toast.makeText(this, "userName can not be empty", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "password can not be empty", Toast.LENGTH_SHORT).show();
            return;
        }
        if (isInstallApp(this, "gaming178.com.baccaratgame")) {//Check to see if your phone has GD88 installed
            Intent intent = new Intent(Intent.ACTION_MAIN);
            ComponentName componentName = new ComponentName("gaming178.com.baccaratgame", "gaming178.com.casinogame.Activity.WelcomeActivity");
            intent.setComponent(componentName);
            intent.putExtra("userName", userName);//Your app Account userName
            intent.putExtra("password", password);//Your app Account password
            intent.putExtra("gameType", gameType);//invariable
            intent.putExtra("web_id", webId);//invariable
            intent.putExtra("language", language);//zh en
            startActivity(intent);
        } else {
            downloadGD88();
        }
    }

    private void downloadGD88() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://www.appgd88.com/androidDownload/gd88.apk");
        intent.setData(content_url);
        startActivity(intent);
    }

    private boolean isInstallApp(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        List<String> packageNames = new ArrayList<String>();
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        return packageNames.contains(packageName);
    }
}
